//
//  PaperQuestionAnalysisFooter.m
//  E-Publishing
//
//  Created by zhuxuhong on 16/8/30.
//
//

#import "PaperQuestionAnalysisFooter.h"

@interface PaperQuestionAnalysisFooter()<UIWebViewDelegate>

@property(nonatomic,copy)PaperQuestion *question;
@property(nonatomic,weak)id<PaperQuestionAnalysisFooterDelegate> delegate;

@end

@implementation PaperQuestionAnalysisFooter
{
    UIWebView *webview;
}

-(instancetype)initWithDelegate: (id)delegate{
    if (self = [super init]) {
        _delegate = delegate;
//        self.backgroundColor = [UIColor redColor];
        webview = [UIWebView new];
        webview.scrollView.scrollEnabled = false;
        webview.delegate = self;
        [self addSubview:webview];
    }
    return self;
}

-(void)loadContentWithQuestion:(PaperQuestion *)question baseUrl:(NSString *)baseUrl{
    _question = question;
    [self setUserAnswer: _question.userAnswer
               category:_question.categoryName
         standardAnswer:_question.answer
               analysis:_question.analysis
                baseUrl: baseUrl];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    webview.frame = self.bounds;
}

-(void)setUserAnswer:(NSString *)userAnswer
            category:(NSString *)category
      standardAnswer:(NSString *)standardAnswer
            analysis:(NSString *)analysis
             baseUrl: (NSString*)baseUrl{
    NSMutableString *str = [NSMutableString string];
//    [str appendString:[self htmlWithTitle:@"【您的答案】" content:userAnswer]];
    [str appendString:[self htmlWithTitle:@"【正确答案】" content:standardAnswer]];
    [str appendString:[self htmlWithTitle:@"【知&nbsp;&nbsp;识&nbsp;&nbsp;点】" content:category]];
    [str appendString:[self htmlWithTitle:@"【试题分析】" content:analysis]];
    
    NSString *html = [NSString stringWithFormat:@"<html><body><div id=\"analysis\" style=\"word-break:break-word;font-size:16px;margin-left:5px;\">%@</div></body></html>",str];
    NSURL *baseURL = [NSURL fileURLWithPath:baseUrl];
    [webview loadHTMLString:html baseURL:baseURL];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    CGFloat h = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"analysis\").offsetHeight;"] floatValue];
    if (h > 0) {
        CGRect frame = webView.frame;
        frame.size.height = h;
        webView.frame = frame;
        
        _question.analysisFooterHeight = h+10;
        [self.delegate analysisFooter:self didLoadWithQuestion:_question];
    }
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return _question.analysisFooterHeight == 0;
}

-(NSString*)htmlWithTitle: (NSString*)title content: (NSString*)content{
    content = content ? content : @"";
    NSString *html = [NSString stringWithFormat:@"<p style=\"line-height:1;\"><span style=\"color:#249a83;\">%@</span>%@</p>",title,content];
    return html;
}

-(void)labledText{
    /*
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@""];
    [attr appendAttributedString: [self titleAttributedStringWithString:@"您的答案: "]];
    [attr appendAttributedString: [self contentAttributedStringWithString:userAnswer]];
    [attr appendAttributedString: [self titleAttributedStringWithString:@"正确答案: "]];
    [attr appendAttributedString: [self contentAttributedStringWithString:standardAnswer]];
    [attr appendAttributedString: [self titleAttributedStringWithString:@"知  识  点: "]];
    [attr appendAttributedString: [self contentAttributedStringWithString:category]];
    [attr appendAttributedString: [self titleAttributedStringWithString:@"问题分析: "]];
    [attr appendAttributedString: [self contentAttributedStringWithString:analysis]];
    
    [label setAttributedText:attr];
    */
}

-(NSAttributedString*)titleAttributedStringWithString: (NSString*)title{
    return [[NSAttributedString alloc] initWithString:title attributes:@{ NSForegroundColorAttributeName: [UIColor colorWithRed:36/255.0 green:154/255.0 blue:131/255.0 alpha:1]}];
}

-(NSAttributedString*)contentAttributedStringWithString: (NSString*)content{
    if (!content) {
        content = @"";
    }
    return [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n",content] attributes:@{ NSForegroundColorAttributeName: [UIColor blackColor]}];
}


@end
